<?php
namespace Home\Controller;
use Think\Cache\Driver\Redis;
class GoodShopcartController extends CommonController
{
    //添加购物车
    function addcart()
    {
        //验证商品id是否有效
        $good_id = to_int(I('post.good_id'));
        $g_id_res = M('good')->where(array("id"=>$good_id))->find();
        if(empty($g_id_res)){
            $this->ajaxReturn(-1);
        }
        $num = to_int(I('post.num'));
        if($num<=0){
            $this->ajaxReturn(-2);
        }
        //状态码0是非代理1件2箱
        $status = I('post.status');
        //不传参就是件
        if(empty($status)||$status == '1'){
            $status = '1';
        }elseif($status !== "2"){
            $this->ajaxReturn(-3);
        }
        //规格
        $spec_str = trim(I("post.spec_str"));
        $spec_str_arr = explode(",",$spec_str);
        //验证规格是否有效
        if(!empty($spec_str)){
            $bool = 0;
            $good_spec = M("good_spec")->where(array("good_id"=>$good_id))->select();
            foreach ($good_spec as $gay => $gel){
                $good_spec_arr = explode(",",$gel['spec_val']);
                foreach($spec_str_arr as $jay){
                    if(in_array($jay,$good_spec_arr)){
                        $bool++;
                    }
                }
            }
            //是否有
            if($bool !== count($spec_str_arr)){
                $this->ajaxReturn(-4);
            }
        }
        //获取拿货价/件
        $good_info = M('good')->where(array('id'=>$good_id,'shelves'=>1))->find();
        $new_rank_id = M('agentBrand')->where( array('brand_id'=>$good_info['brand_id'], 'agent_id'=>session('user_id')))->getField('rank_id');
        $res = M('good_rank_price')->where(array('good_id' => $good_id, 'rank_id' => $new_rank_id))->find();
        //判断是否是普通用户
        $agenttype = M('agent')->where(array('agent_id'=>session("user_id")))->getField('is_agent');
        //判断品牌是否是非代理品牌
        $agent_brand = M('brand')->where(array("id"=>$good_info['brand_id']))->getField("agent_brand");
        if($agenttype == 2||$agent_brand == 0){
             $cart_info['price'] = $good_info['price'];
             $status = '0';
        }else{
            //防止有商家没填拿货价，就用商品售价
            if(!$res){
                $cart_info['price'] = $good_info['price'];
            }else{
                if($status ==1){
                    $cart_info['price'] = $res['unit_amt'];
                }else{
                    $cart_info['price'] = $res['batch_amt'];
                }
            }
        }
        $cart_info['agent_id'] = session('user_id');
        $cart_info['cart_id'] = 0;
        $cart_info['good_id'] = $good_id;
        $cart_info['good_name']= $good_info['g_name'];
        $cart_info['brand_id'] = $good_info['brand_id'];
        $cart_info['good_pic'] = $good_info['img'];
        $cart_info['good_num'] = $num;
        $cart_info['status'] = $status;
        $cart_info['spec_str'] = serialize($spec_str);

        $app = M('shop_cart')->where(array('agent_id'=>session('user_id')))->select();
        if(!$app){
            $cart_info['cart_id'] = 0;
            M('shop_cart')->add($cart_info);
            $this->ajaxReturn(1);
        } else{
            //遍历当前的购物车数组
            //遍历每个商品信息数组的0值，如果键值为0且货号相同则购物车存在相同货品
            $reh = 0;
            foreach ($app as $key=>$val) {
                if ($good_id == $val['good_id']&&$spec_str == unserialize($val['spec_str'])){
                    if($status == $val['status']){
                        $data['good_num'] = $val['good_num'] + $num ;
                        M('shop_cart')->where(array('agent_id'=>session('user_id'),'good_id'=>$val['good_id'],'status'=>$val['status']))
                            ->save($data);
                        $reh = 1;
                    }
                }
            }
            if($reh == 0){
                //返回数组键名倒序取最大
                $ar_keys = array_keys($app);
                rsort($ar_keys);
                $max_array_keyid = $ar_keys[0]+1;
                $cart_info['cart_id'] = $max_array_keyid;
                M('shop_cart')->add($cart_info);
                $this->ajaxReturn(1);
            }
            $this->ajaxReturn(1);
        }
    }

    public function shopcart()
    {
        if (IS_AJAX) {
            //删除购物车
            $cart_id = I('post.cookie_id');
            M('shop_cart')->where(array('agent_id'=>session('user_id'),'cart_id'=>$cart_id))->delete();
            $pa = M("shop_cart")->where(array("agent_id"=>session("user_id")))->select();
            $i=0;
            foreach ($pa as $puck => $vine){
                M("shop_cart")->where(array("agent_id"=>session("user_id"),"id"=>$vine['id']))->setField("cart_id",$i);
                $i++;
            }
            $this->ajaxReturn(1);
        }else {
            $res = M('shop_cart')->where(array('agent_id'=> session('user_id')))->order('cart_id DESC')->select();
//          查询商品库里的商品核对是否有新更新
            foreach ($res as $key => $val){
//                $val['spec_str'] = unserialize($val['spec_str']);
                $res[$key]['spec_str'] = unserialize($val['spec_str']);
                $good = M('good')->where(array('id'=>$val['good_id']))->find();
//          判断商品是否下架
                if($good['shelves'] == -1){
                    $val['expired'] = 1;
                }else{
//          不下架跟新当时的价格
                    $val['expired'] = -1;
                    if($val['status'] == 0){
                        $val['price']  = $good['price'];
                    }elseif ($val['status'] == 1){
                        $arr = single_good_amt($good);
                        $val['price'] = $arr['unit_amt'];
                    }elseif ($val['status'] == 2){
                        $arr = single_good_amt($good);
                        $val['price'] = $arr['batch_amt'];
                    }
                }
                M('shop_cart')->where(array('good_id'=>$val['good_id']))->save($val);
                $app[$key] = $res[$key];
            }
            $this->assign('res', $app);
            $status = "none";
            $self_address = M("agent_address")->where("state = 0 AND agent_id = %d",session("user_id"))->find();
            if($self_address){
                $status = "has";
            }
            $this->assign("self_address" ,$status);
            $this->display();
        }
    }

}